﻿using Sasc.PV.BLL.Annotation;
using Sasc.PV.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sasc.PV.BLL.ZhuDing
{
    [CheJian("铸锭车间")]
    public class ZhuDingCheJianManagement : FanGongableCheJianManagement<ZhuDingFanGongGongXu, ZhuDingGongXuTarget>
    {
        #region CreateGongXuRecModel
        protected override IPvGongXuRecModel CreateGongXuRecModel<TCheJian>(CheJianGongXuBase<TCheJian, ZhuDingGongXuTarget> gongxu, ZhuDingGongXuTarget target)
        {
            return CreateGongXuRecModelInternal(gongxu as ZhuDingGongXuBase, target);
        }

        protected PvZDGongXuRec CreateGongXuRecModelInternal(ZhuDingGongXuBase gongxu, ZhuDingGongXuTarget target)
        {
            var currentFanGongVersion = GetCurrentFanGongVersion(gongxu, target);
            //当前是返工工序
            if (typeof(ICheJianFanGongGongXu).IsAssignableFrom(this.GetType()))
                currentFanGongVersion++;

            return new PvZDGongXuRec
            {
                CreatedBy = LoginUserName,
                CreatedDate = DateTime.Now,
                GanGuoBianHao = target.GanGuoBianHao,
                GongXuName = gongxu.GongXuName,
                FanGongVersion = currentFanGongVersion, 
                Note = string.Empty,
                TargetType = 0,
                XiaoDingBianHao = target.XiaoDingBianHao
            };
        }
        #endregion

        #region ExistsGongXuTarget
        protected override bool ExistsGongXuTarget(ZhuDingGongXuTarget target, out string reason)
        {
            reason = string.Empty;
            var result = false;
            if (target.GanGuoBianHaoType == GongXuTargetDataType.Required)
            {
                if (string.IsNullOrEmpty(target.GanGuoBianHao))
                {
                    reason = "不存在编号为空的坩埚。";
                    result = false;
                }
                else
                {
                    result = DbHelperSQL.Exists(string.Format("SELECT 1 FROM PvcGanGuo WHERE BianHao='{0}'", target.GanGuoBianHao));
                    if (!result)
                        reason = string.Format("不存在坩埚编号:{0}.", target.GanGuoBianHao);
                }
            }
            else if (target.GanGuoBianHaoType == GongXuTargetDataType.Optional && !string.IsNullOrEmpty(target.GanGuoBianHao))
            {
                result = DbHelperSQL.Exists(string.Format("SELECT 1 FROM PvcGanGuo WHERE BianHao='{0}'", target.GanGuoBianHao));
                if (!result)
                    reason = string.Format("不存在坩埚编号:{0}.", target.GanGuoBianHao);
            }

            if (result)
            {
                if (target.XiaoDingBianHaoType == GongXuTargetDataType.Required)
                {
                    if (string.IsNullOrEmpty(target.XiaoDingBianHao))
                    {
                        reason = "不存在编号为空的小锭。";
                        result = false;
                    }
                    else
                    {
                        result = DbHelperSQL.Exists(string.Format("SELECT 1 FROM PvcXiaoDing WHERE BianHao='{0}'", target.XiaoDingBianHao));
                        if (!result)
                            reason = string.Format("不存在小锭编号:{0}.", target.XiaoDingBianHao);
                    }
                }
                else if (target.XiaoDingBianHaoType == GongXuTargetDataType.Optional && !string.IsNullOrEmpty(target.XiaoDingBianHao))
                {
                    result = DbHelperSQL.Exists(string.Format("SELECT 1 FROM PvcXiaoDing WHERE BianHao='{0}'", target.XiaoDingBianHao));
                    if (!result)
                        reason = string.Format("不存在小锭编号:{0}.", target.XiaoDingBianHao);
                }
            }

            return result;
        }
        #endregion
    }
}
